/*
 * buy_ticket_cb.c
 *
 *  Created on: 2013-2-5
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "../head/gui_callback/buy_ticket_cb.h"
#include "../head/gui_callback/global_cb.h"
#include "../head/communicate_with_server/traininfo_transfer.h"
#include "../head/common_struct/traininfo.h"
#include "../head/communicate_with_server/userinfo_transfer.h"

//判断出发时间和返回时间是否正确
bool is_depart_back_date_ok(const char *departDate, const char *backDate);

extern GtkBuilder *builder;

bool is_buy = false; //当用户选择往返时，用来标记是否已经买了去的车票了

enum {
	SEARCH_TICKET_LEFT_COL_TRAINID = 0,
	SEARCH_TICKET_LEFT_COL_ISFIRST,
	SEARCH_TICKET_LEFT_COL_STARTSTATION,
	SEARCH_TICKET_LEFT_COL_STARTTIME,
	SEARCH_TICKET_LEFT_COL_ISLAST,
	SEARCH_TICKET_LEFT_COL_ENDSTATION,
	SEARCH_TICKET_LEFT_COL_ENDTIME,
	SEARCH_TICKET_LEFT_COL_TIMELAST,
	SEARCH_TICKET_LEFT_COL_TICKETINFO,
	SEARCH_TICKET_LEFT_COL_DEPARTDATE,
	SEARCH_TICKET_LEFT_COL_PRICE,
};

//处理工具栏中的购票的点击事件
void toolbutton_buy_ticket_clicked_cb(GtkToolButton *toolbutton,
		gpointer user_data) {
	GtkRadioButton *radiobutton_buy_ticket_single;
	GtkRadioButton *radiobutton_buy_ticket_back;
	GtkEntry *entry_buy_ticket_startaddress;
	GtkEntry *entry_buy_ticket_endaddress;
	GtkEntry *entry_buy_ticket_startdate;
	GtkCalendar *calendar_buy_ticket_startdate;
	GtkComboBox *combobox_buy_ticket_begintime;
	GtkEntry *entry_buy_ticket_train;
	GtkLabel *label_buy_ticket_backdate_warn;
	GtkLabel *label_buy_ticket_backdate_symbol;
	GtkEntry *entry_buy_ticket_backdate;
	GtkCalendar *calendar_buy_ticket_backdate;
	GtkLabel *label_buy_ticket_backtime_symbol;
	GtkComboBox *combobox_buy_ticket_backtime;
	GtkRadioButton *radiobutton_buy_ticket_stationtype_all;
	GtkRadioButton *radiobutton_buy_ticket_stationtype_start;
	GtkRadioButton *radiobutton_buy_ticket_stationtype_pass;
	GtkCheckButton *checkbutton_buy_ticket_traintype_DG;
	GtkCheckButton *checkbutton_buy_ticket_traintype_Z;
	GtkCheckButton *checkbutton_buy_ticket_traintype_T;
	GtkCheckButton *checkbutton_buy_ticket_traintype_K;
	GtkCheckButton *checkbutton_buy_ticket_traintype_other;
	GtkTreeView *treeview_buy_ticket;

	radiobutton_buy_ticket_single =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_buy_ticket_single"));
	radiobutton_buy_ticket_back =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_buy_ticket_back"));
	entry_buy_ticket_startaddress =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_startaddress"));
	entry_buy_ticket_endaddress =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_endaddress"));
	entry_buy_ticket_startdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_startdate"));
	calendar_buy_ticket_startdate = GTK_CALENDAR(
			gtk_builder_get_object(builder, "calendar_buy_ticket_startdate"));
	combobox_buy_ticket_begintime =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_buy_ticket_begintime"));
	entry_buy_ticket_train =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_train"));
	label_buy_ticket_backdate_warn =
			GTK_LABEL(gtk_builder_get_object(builder,"label_buy_ticket_backdate_warn"));
	label_buy_ticket_backdate_symbol =
			GTK_LABEL(gtk_builder_get_object(builder,"label_buy_ticket_backdate_symbol"));
	entry_buy_ticket_backdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_backdate"));
	calendar_buy_ticket_backdate = GTK_CALENDAR(
			gtk_builder_get_object(builder, "calendar_buy_ticket_backdate"));
	label_buy_ticket_backtime_symbol =
			GTK_LABEL(gtk_builder_get_object(builder,"label_buy_ticket_backtime_symbol"));
	combobox_buy_ticket_backtime =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_buy_ticket_backtime"));
	radiobutton_buy_ticket_stationtype_all =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_buy_ticket_stationtype_all"));
	radiobutton_buy_ticket_stationtype_start =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_buy_ticket_stationtype_start"));
	radiobutton_buy_ticket_stationtype_pass =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_buy_ticket_stationtype_pass"));
	checkbutton_buy_ticket_traintype_DG =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_buy_ticket_traintype_DG"));
	checkbutton_buy_ticket_traintype_Z =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_buy_ticket_traintype_Z"));
	checkbutton_buy_ticket_traintype_T =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_buy_ticket_traintype_T"));
	checkbutton_buy_ticket_traintype_K =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_buy_ticket_traintype_K"));
	checkbutton_buy_ticket_traintype_other =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_buy_ticket_traintype_other"));
	treeview_buy_ticket = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_buy_ticket"));

	gtk_widget_set_sensitive(GTK_WIDGET(radiobutton_buy_ticket_single), true);
	gtk_widget_set_sensitive(GTK_WIDGET(radiobutton_buy_ticket_back), true);
	gtk_widget_set_sensitive(GTK_WIDGET(entry_buy_ticket_startaddress), true);
	gtk_widget_set_sensitive(GTK_WIDGET(entry_buy_ticket_endaddress), true);
	gtk_widget_set_sensitive(GTK_WIDGET(entry_buy_ticket_startdate), true);
	gtk_widget_set_sensitive(GTK_WIDGET(combobox_buy_ticket_begintime), true);

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radiobutton_buy_ticket_single
	), true);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radiobutton_buy_ticket_back
	), false);
	gtk_entry_set_text(entry_buy_ticket_startaddress, "");
	gtk_entry_set_text(entry_buy_ticket_endaddress, "");

	struct tm *tm_ptr;
	time_t the_time;
	(void) time(&the_time);
	tm_ptr = localtime(&the_time);
	char date[TRAIN_NUMBER_DATE_LEN];
	memset(date, 0, sizeof(date));
	sprintf(date, "%04d-%02d-%02d", tm_ptr->tm_year + 1900, tm_ptr->tm_mon + 1,
			tm_ptr->tm_mday);
	gtk_entry_set_text(entry_buy_ticket_startdate, date);
	gtk_calendar_select_month(calendar_buy_ticket_startdate, tm_ptr->tm_mon,
			tm_ptr->tm_year + 1900);
	gtk_calendar_select_day(calendar_buy_ticket_startdate, tm_ptr->tm_mday);

	gtk_combo_box_set_active(combobox_buy_ticket_begintime, 0);
	gtk_entry_set_text(entry_buy_ticket_train, "");

	gtk_entry_set_text(entry_buy_ticket_backdate, date);
	gtk_calendar_select_month(calendar_buy_ticket_backdate, tm_ptr->tm_mon,
			tm_ptr->tm_year + 1900);
	gtk_calendar_select_day(calendar_buy_ticket_backdate, tm_ptr->tm_mday);

	gtk_combo_box_set_active(combobox_buy_ticket_backtime, 0);

	gtk_widget_hide(GTK_WIDGET(label_buy_ticket_backdate_warn) );
	gtk_widget_hide(GTK_WIDGET(label_buy_ticket_backdate_symbol) );
	gtk_widget_hide(GTK_WIDGET(entry_buy_ticket_backdate) );
	gtk_widget_hide(GTK_WIDGET(label_buy_ticket_backtime_symbol) );
	gtk_widget_hide(GTK_WIDGET(combobox_buy_ticket_backtime) );

	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(radiobutton_buy_ticket_stationtype_all
			), true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(radiobutton_buy_ticket_stationtype_start
			), false);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(radiobutton_buy_ticket_stationtype_pass
			), false);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_buy_ticket_traintype_DG
			), true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_buy_ticket_traintype_Z
			), true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_buy_ticket_traintype_T
			), true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_buy_ticket_traintype_K
			), true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_buy_ticket_traintype_other
			), true);

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_buy_ticket);
	gtk_list_store_clear(GTK_LIST_STORE(model) );

	is_buy = false;

	window_show(BUY_TICKET);
}

//处理车票查询按钮的点击事件
void button_buy_ticket_search_clicked_cb(GtkButton *button, gpointer user_data) {
	GtkRadioButton *radiobutton_buy_ticket_single;
	GtkEntry *entry_buy_ticket_startaddress;
	GtkEntry *entry_buy_ticket_endaddress;
	GtkEntry *entry_buy_ticket_startdate;
	GtkComboBox *combobox_buy_ticket_begintime;
	GtkEntry *entry_buy_ticket_train;
	GtkEntry *entry_buy_ticket_backdate;
	GtkComboBox *combobox_buy_ticket_backtime;
	GtkRadioButton *radiobutton_buy_ticket_stationtype_all;
	GtkRadioButton *radiobutton_buy_ticket_stationtype_start;
	GtkRadioButton *radiobutton_buy_ticket_stationtype_pass;
	GtkCheckButton *checkbutton_buy_ticket_traintype_DG;
	GtkCheckButton *checkbutton_buy_ticket_traintype_Z;
	GtkCheckButton *checkbutton_buy_ticket_traintype_T;
	GtkCheckButton *checkbutton_buy_ticket_traintype_K;
	GtkCheckButton *checkbutton_buy_ticket_traintype_other;
	GtkTreeView *treeview_buy_ticket;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	radiobutton_buy_ticket_single =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_buy_ticket_single"));
	entry_buy_ticket_startaddress =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_startaddress"));
	entry_buy_ticket_endaddress =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_endaddress"));
	entry_buy_ticket_startdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_startdate"));
	combobox_buy_ticket_begintime =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_buy_ticket_begintime"));
	entry_buy_ticket_train =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_train"));
	entry_buy_ticket_backdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_backdate"));
	combobox_buy_ticket_backtime =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_buy_ticket_backtime"));
	radiobutton_buy_ticket_stationtype_all =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_buy_ticket_stationtype_all"));
	radiobutton_buy_ticket_stationtype_start =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_buy_ticket_stationtype_start"));
	radiobutton_buy_ticket_stationtype_pass =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_buy_ticket_stationtype_pass"));
	checkbutton_buy_ticket_traintype_DG =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_buy_ticket_traintype_DG"));
	checkbutton_buy_ticket_traintype_Z =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_buy_ticket_traintype_Z"));
	checkbutton_buy_ticket_traintype_T =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_buy_ticket_traintype_T"));
	checkbutton_buy_ticket_traintype_K =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_buy_ticket_traintype_K"));
	checkbutton_buy_ticket_traintype_other =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_buy_ticket_traintype_other"));
	treeview_buy_ticket = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_buy_ticket"));

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_buy_ticket);
	gtk_list_store_clear(GTK_LIST_STORE(model) );

	const gchar *startAddress;
	const gchar *endAddress;
	const gchar *trainID;
	const gchar *departDate;
	const gchar *backDate;
	int startTimeRangeLowHour;
	int startTimeRangeLowMinute;
	int startTimeRangeHighHour;
	int startTimeRangeHighMinute;

	if (gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(radiobutton_buy_ticket_single) )) { //单程票查询
		startAddress = gtk_entry_get_text(entry_buy_ticket_startaddress);
		endAddress = gtk_entry_get_text(entry_buy_ticket_endaddress);
		if (strlen(startAddress) == 0) {
			gtk_show_warning(global_window, "%s", "出发地不能为空", "输入提醒");
			return;
		}
		if (strlen(endAddress) == 0) {
			gtk_show_warning(global_window, "%s", "目的地不能为空", "输入提醒");
			return;
		}
		trainID = gtk_entry_get_text(entry_buy_ticket_train);
		departDate = gtk_entry_get_text(entry_buy_ticket_startdate);

		const gchar *startTimeRange;
		GtkTreeModel *model_timerange = gtk_combo_box_get_model(
				combobox_buy_ticket_begintime);
		GtkTreeIter iter_timerange;
		gtk_combo_box_get_active_iter(combobox_buy_ticket_begintime,
				&iter_timerange);
		gtk_tree_model_get(model_timerange, &iter_timerange, 0, &startTimeRange,
				-1);
		sscanf(startTimeRange, "%02d:%02d--%02d:%02d", &startTimeRangeLowHour,
				&startTimeRangeLowMinute, &startTimeRangeHighHour,
				&startTimeRangeHighMinute);
	} else { //往返车票查询
		if (!is_buy) { //往票查询
			startAddress = gtk_entry_get_text(entry_buy_ticket_startaddress);
			endAddress = gtk_entry_get_text(entry_buy_ticket_endaddress);
			if (strlen(startAddress) == 0) {
				gtk_show_warning(global_window, "%s", "出发地不能为空", "输入提醒");
				return;
			}
			if (strlen(endAddress) == 0) {
				gtk_show_warning(global_window, "%s", "目的地不能为空", "输入提醒");
				return;
			}
			trainID = gtk_entry_get_text(entry_buy_ticket_train);
			departDate = gtk_entry_get_text(entry_buy_ticket_startdate);
			backDate = gtk_entry_get_text(entry_buy_ticket_backdate);

			if (is_depart_back_date_ok(departDate, backDate)) {
				gtk_show_warning(global_window, "%s", "出发日期不能晚于返回日期", "输入提醒");
				return;
			}

			const gchar *startTimeRange;
			GtkTreeModel *model_timerange = gtk_combo_box_get_model(
					combobox_buy_ticket_begintime);
			GtkTreeIter iter_timerange;
			gtk_combo_box_get_active_iter(combobox_buy_ticket_begintime,
					&iter_timerange);
			gtk_tree_model_get(model_timerange, &iter_timerange, 0,
					&startTimeRange, -1);
			sscanf(startTimeRange, "%02d:%02d--%02d:%02d",
					&startTimeRangeLowHour, &startTimeRangeLowMinute,
					&startTimeRangeHighHour, &startTimeRangeHighMinute);
		} else { //返票查询
			startAddress = gtk_entry_get_text(entry_buy_ticket_endaddress);
			endAddress = gtk_entry_get_text(entry_buy_ticket_startaddress);
			trainID = gtk_entry_get_text(entry_buy_ticket_train);
			departDate = gtk_entry_get_text(entry_buy_ticket_startdate);
			backDate = gtk_entry_get_text(entry_buy_ticket_backdate);

			if (is_depart_back_date_ok(departDate, backDate)) {
				gtk_show_warning(global_window, "%s", "出发日期不能晚于返回日期", "输入提醒");
				return;
			}

			const gchar *startTimeRange;
			GtkTreeModel *model_timerange = gtk_combo_box_get_model(
					combobox_buy_ticket_backtime);
			GtkTreeIter iter_timerange;
			gtk_combo_box_get_active_iter(combobox_buy_ticket_backtime,
					&iter_timerange);
			gtk_tree_model_get(model_timerange, &iter_timerange, 0,
					&startTimeRange, -1);
			sscanf(startTimeRange, "%02d:%02d--%02d:%02d",
					&startTimeRangeLowHour, &startTimeRangeLowMinute,
					&startTimeRangeHighHour, &startTimeRangeHighMinute);
		}
	}

	int passStationType = TYPE_PASS_STATION_ALL;
	gboolean isPassStationTypeALL = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_buy_ticket_stationtype_all) );
	if (isPassStationTypeALL) {
		passStationType = TYPE_PASS_STATION_ALL;
	}
	gboolean isPassStationTypeBegin = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_buy_ticket_stationtype_start) );
	if (isPassStationTypeBegin) {
		passStationType = TYPE_PASS_STATION_BEGIN;
	}
	gboolean isPassStationTypePass = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_buy_ticket_stationtype_pass) );
	if (isPassStationTypePass) {
		passStationType = TYPE_PASS_STATION_PASS;
	}

	int trainType = TYPE_TRAIN_TYPE_NONE;
	gboolean isTrainTypeDG = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_buy_ticket_traintype_DG) );
	if (isTrainTypeDG) {
		trainType += TYPE_TRAIN_TYPE_DG;
	}
	gboolean isTrainTypeZ = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_buy_ticket_traintype_Z) );
	if (isTrainTypeZ) {
		trainType += TYPE_TRAIN_TYPE_Z;
	}
	gboolean isTrainTypeT = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_buy_ticket_traintype_T) );
	if (isTrainTypeT) {
		trainType += TYPE_TRAIN_TYPE_T;
	}
	gboolean isTrainTypeK = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_buy_ticket_traintype_K) );
	if (isTrainTypeK) {
		trainType += TYPE_TRAIN_TYPE_K;
	}
	gboolean isTrainTypeOther = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_buy_ticket_traintype_other) );
	if (isTrainTypeOther) {
		trainType += TYPE_TRAIN_TYPE_OTHER;
	}

	if (trainType == TYPE_TRAIN_TYPE_NONE) {
		gtk_show_warning(global_window, "%s", "请选择列车类型", "输入提醒");
		return;
	}

	struct ticket_left_request request;
	memset(&request, 0, sizeof(request));
	g_utf8_strncpy(request.startAddress, startAddress,
			g_utf8_strlen(startAddress, STATIONS_CITY_LEN));
	g_utf8_strncpy(request.endAddress, endAddress,
			g_utf8_strlen(endAddress, STATIONS_CITY_LEN));
	strncpy(request.trainID, trainID, strlen(trainID));
	strncpy(request.departDate, departDate, strlen(departDate));
	sprintf(request.timeRangeLow, "%02d:%02d", startTimeRangeLowHour,
			startTimeRangeLowMinute);
	sprintf(request.timeRangeHigh, "%02d:%02d", startTimeRangeHighHour,
			startTimeRangeHighMinute);
	request.passStationType = passStationType;
	request.trainType = trainType;

	TicketList ticketlist; //声明列表
	InitializeListTicket(&ticketlist); //初始化列表
	bool search_result = search_ticket_left(&request, &ticketlist);
	if (search_result) {
		char departStation[STATIONS_STATION_NAME_LEN] = "first.gif";
		char arriveStation[STATIONS_STATION_NAME_LEN] = "last.gif";
		char passStation[STATIONS_STATION_NAME_LEN] = "pass.png";
		char ticketNone[10] = "无";
		char ticketNoStartSale[10] = "*";

		char isfirst[STATIONS_STATION_NAME_LEN];
		char islast[STATIONS_STATION_NAME_LEN];
		char ticketinfo[10];
		memset(isfirst, 0, sizeof(isfirst));
		memset(islast, 0, sizeof(islast));
		strncpy(isfirst, passStation, strlen(passStation));
		strncpy(islast, passStation, strlen(passStation));
		memset(ticketinfo, 0, sizeof(ticketinfo));
		TicketNode *node = ticketlist;
		GtkTreeIter iter;
		while (node != NULL ) {
			TicketItem item = node->item;
			if (item.isDepartStation) {
				memset(isfirst, 0, sizeof(isfirst));
				strncpy(isfirst, departStation, strlen(departStation));
			}
			if (item.isArriveStation) {
				memset(islast, 0, sizeof(islast));
				strncpy(islast, arriveStation, strlen(arriveStation));
			}
			if (item.isStartSale) {
				if (item.ticketLeft == 0) {
					sprintf(ticketinfo, "%s", ticketNone);
				} else {
					sprintf(ticketinfo, "%d", item.ticketLeft);
				}
			} else {
				sprintf(ticketinfo, "%s", ticketNoStartSale);
			}

			GdkPixbuf* icon_first = gdk_pixbuf_new_from_file(isfirst, NULL );
			GdkPixbuf* icon_last = gdk_pixbuf_new_from_file(islast, NULL );

			gtk_list_store_append(GTK_LIST_STORE(model), &iter);
			gtk_list_store_set(GTK_LIST_STORE(model), &iter,
					SEARCH_TICKET_LEFT_COL_TRAINID, item.trainID,
					SEARCH_TICKET_LEFT_COL_ISFIRST, icon_first,
					SEARCH_TICKET_LEFT_COL_STARTSTATION, item.startStation,
					SEARCH_TICKET_LEFT_COL_STARTTIME, item.startTime,
					SEARCH_TICKET_LEFT_COL_ISLAST, icon_last,
					SEARCH_TICKET_LEFT_COL_ENDSTATION, item.endStation,
					SEARCH_TICKET_LEFT_COL_ENDTIME, item.endTime,
					SEARCH_TICKET_LEFT_COL_TIMELAST, item.consumeTime,
					SEARCH_TICKET_LEFT_COL_TICKETINFO, ticketinfo,
					SEARCH_TICKET_LEFT_COL_DEPARTDATE, item.departDate,
					SEARCH_TICKET_LEFT_COL_PRICE, item.price, -1);

			node = node->next;
		}
	} else {
		gtk_show_info(global_window, "%s", "未找到相应信息", "信息提示");
	}
	EmptyTheListTicket(&ticketlist);
}

//处理单程按钮的选择事件
void radiobutton_buy_ticket_single_toggled_cb(GtkToggleButton *togglebutton,
		gpointer user_data) {
	GtkLabel *label_buy_ticket_backdate_warn;
	GtkLabel *label_buy_ticket_backdate_symbol;
	GtkEntry *entry_buy_ticket_backdate;
	GtkCalendar *calendar_buy_ticket_backdate;
	GtkLabel *label_buy_ticket_backtime_symbol;
	GtkComboBox *combobox_buy_ticket_backtime;

	label_buy_ticket_backdate_warn =
			GTK_LABEL(gtk_builder_get_object(builder,"label_buy_ticket_backdate_warn"));
	label_buy_ticket_backdate_symbol =
			GTK_LABEL(gtk_builder_get_object(builder,"label_buy_ticket_backdate_symbol"));
	entry_buy_ticket_backdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_backdate"));
	calendar_buy_ticket_backdate = GTK_CALENDAR(
			gtk_builder_get_object(builder, "calendar_buy_ticket_backdate"));
	label_buy_ticket_backtime_symbol =
			GTK_LABEL(gtk_builder_get_object(builder,"label_buy_ticket_backtime_symbol"));
	combobox_buy_ticket_backtime =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_buy_ticket_backtime"));

	gboolean isactive = gtk_toggle_button_get_active(togglebutton);
	if (isactive) {
		gtk_widget_hide(GTK_WIDGET(label_buy_ticket_backdate_warn) );
		gtk_widget_hide(GTK_WIDGET(label_buy_ticket_backdate_symbol) );
		gtk_widget_hide(GTK_WIDGET(entry_buy_ticket_backdate) );
		gtk_widget_hide(GTK_WIDGET(label_buy_ticket_backtime_symbol) );
		gtk_widget_hide(GTK_WIDGET(combobox_buy_ticket_backtime) );
	} else {
		struct tm *tm_ptr;
		time_t the_time;
		(void) time(&the_time);
		tm_ptr = localtime(&the_time);
		char date[TRAIN_NUMBER_DATE_LEN];
		memset(date, 0, sizeof(date));
		sprintf(date, "%04d-%02d-%02d", tm_ptr->tm_year + 1900,
				tm_ptr->tm_mon + 1, tm_ptr->tm_mday);
		gtk_entry_set_text(entry_buy_ticket_backdate, date);
		gtk_calendar_select_month(calendar_buy_ticket_backdate, tm_ptr->tm_mon,
				tm_ptr->tm_year + 1900);
		gtk_calendar_select_day(calendar_buy_ticket_backdate, tm_ptr->tm_mday);

		gtk_combo_box_set_active(combobox_buy_ticket_backtime, 0);

		gtk_widget_show(GTK_WIDGET(label_buy_ticket_backdate_warn) );
		gtk_widget_show(GTK_WIDGET(label_buy_ticket_backdate_symbol) );
		gtk_widget_show(GTK_WIDGET(entry_buy_ticket_backdate) );
		gtk_widget_show(GTK_WIDGET(label_buy_ticket_backtime_symbol) );
		gtk_widget_show(GTK_WIDGET(combobox_buy_ticket_backtime) );
	}
	is_buy = false;
}

//处理列表每行的双击事件
void treeview_buy_ticket_row_activated_cb(GtkTreeView *view, GtkTreePath *path,
		GtkTreeViewColumn *col, gpointer userdata) {
	GtkRadioButton *radiobutton_buy_ticket_single;
	GtkRadioButton *radiobutton_buy_ticket_back;
	GtkEntry *entry_buy_ticket_startaddress;
	GtkEntry *entry_buy_ticket_endaddress;
	GtkEntry *entry_buy_ticket_startdate;
	GtkComboBox *combobox_buy_ticket_begintime;
	GtkWindow *global_window;

	radiobutton_buy_ticket_single =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_buy_ticket_single"));
	radiobutton_buy_ticket_back =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_buy_ticket_back"));
	entry_buy_ticket_startaddress =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_startaddress"));
	entry_buy_ticket_endaddress =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_endaddress"));
	entry_buy_ticket_startdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_buy_ticket_startdate"));
	combobox_buy_ticket_begintime =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_buy_ticket_begintime"));
	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	GtkTreeIter iter;
	GtkTreeModel *model;
	model = gtk_tree_view_get_model(view);

	if (gtk_tree_model_get_iter(model, &iter, path)) {
		gchar *ticketinfo;
		gtk_tree_model_get(model, &iter, SEARCH_TICKET_LEFT_COL_TICKETINFO,
				&ticketinfo, -1);
		if (strcmp(ticketinfo, "无") == 0) {
			gtk_show_warning(global_window, "%s", "该车次车票已售完", "购票提醒");
			return;
		} else if (strcmp(ticketinfo, "*") == 0) {
			gtk_show_warning(global_window, "%s", "该车次未到起售时间", "购票提醒");
			return;
		}
		gchar *trainID;
		gtk_tree_model_get(model, &iter, SEARCH_TICKET_LEFT_COL_TRAINID,
				&trainID, -1);
		gchar *departDate;
		gtk_tree_model_get(model, &iter, SEARCH_TICKET_LEFT_COL_DEPARTDATE,
				&departDate, -1);
		gchar *departStation;
		gtk_tree_model_get(model, &iter, SEARCH_TICKET_LEFT_COL_STARTSTATION,
				&departStation, -1);
		gchar *arriveStation;
		gtk_tree_model_get(model, &iter, SEARCH_TICKET_LEFT_COL_ENDSTATION,
				&arriveStation, -1);
		gchar *price;
		gtk_tree_model_get(model, &iter, SEARCH_TICKET_LEFT_COL_PRICE, &price,
				-1);
		float ticket_price;
		sscanf(price, "%f", &ticket_price);

		int len = strlen("确定要购买车次的车票？") + TRAIN_NUMBER_TRAINID_LEN;
		char message[len];
		memset(message, 0, sizeof(message));

		sprintf(message, "确定要购买%s车次的车票？", trainID);
		bool sure_buy = gtk_show_question(global_window, "%s", message, "信息询问");
		if (sure_buy) {
			struct userinfo *user = get_user_info();
			struct buy_ticket_request request;
			memset(&request, 0, sizeof(request));
			strncpy(request.trainID, trainID, strlen(trainID));
			strncpy(request.username, user->username, strlen(user->username));
			strncpy(request.departStation, departStation,
					strlen(departStation));
			strncpy(request.arriveStation, arriveStation,
					strlen(arriveStation));
			strncpy(request.departDate, departDate, strlen(departDate));
			request.price = ticket_price;

			struct ticket_saled_info ticket;
			memset(&ticket, 0, sizeof(ticket));
			bool is_buy_ok = buy_ticket(&request, &ticket);
			if (is_buy_ok) {
				len =
						strlen(
								"恭喜您，购票成功，车票的信息如下：\n姓名：\n证件类型：\n证件号码：\n订单号：\n车次：\n车厢号：\n座位号：\n发站：\n到站：\n出发日期：\n票价：\n");
				len += USERNAME_LEN + CREDIT_TYPE_LEN + CREDIT_ID_LEN
						+ ORDERS_ORDERID_LEN + ORDERS_TRAINID_LEN
						+ ORDERS_COMPARTMENTNUM_LEN + ORDERS_SEATNUM_LEN
						+ ORDERS_DEPART_STATION_LEN + ORDERS_ARRIVE_STATION_LEN
						+ ORDERS_DEPARTDATE_LEN + ORDERS_PRICE;
				char order_message[len];
				memset(order_message, 0, sizeof(order_message));
				sprintf(order_message,
						"恭喜您，购票成功，车票的信息如下：\n姓名：%s\n证件类型：%s\n证件号码：%s\n订单号：%s\n车次：%s\n车厢号：%s\n座位号：%s\n发站：%s\n到站：%s\n出发日期：%s\n票价：%s元\n",
						user->name, user->creditType, user->creditID,
						ticket.order.orderID, ticket.order.trainID,
						ticket.order.compartmentNum, ticket.order.seatNum,
						ticket.order.departStation, ticket.order.arriveStation,
						ticket.order.departDate, ticket.order.price);
				gtk_show_info(global_window, "%s", order_message, "车票信息");

				if (gtk_toggle_button_get_active(
						GTK_TOGGLE_BUTTON(radiobutton_buy_ticket_single) )) {
					toolbutton_buy_ticket_clicked_cb(NULL, NULL );
				} else {
					if (!is_buy) {
						is_buy = true;
						gtk_widget_set_sensitive(
								GTK_WIDGET(radiobutton_buy_ticket_single),
								false);
						gtk_widget_set_sensitive(
								GTK_WIDGET(radiobutton_buy_ticket_back), false);
						gtk_widget_set_sensitive(
								GTK_WIDGET(entry_buy_ticket_startaddress),
								false);
						gtk_widget_set_sensitive(
								GTK_WIDGET(entry_buy_ticket_endaddress), false);
						gtk_widget_set_sensitive(
								GTK_WIDGET(entry_buy_ticket_startdate), false);
						gtk_widget_set_sensitive(
								GTK_WIDGET(combobox_buy_ticket_begintime),
								false);
					} else {
						toolbutton_buy_ticket_clicked_cb(NULL, NULL );
					}
				}
			} else {
				gtk_show_info(global_window, "%s", "购票失败，请重试", "信息提示");
			}
		}
		g_free(ticketinfo);
		g_free(trainID);
		g_free(departDate);
		g_free(departStation);
		g_free(arriveStation);
		g_free(price);
	}
}

//判断出发时间和返回时间是否正确
bool is_depart_back_date_ok(const char *departDate, const char *backDate) {
	bool iserror = false;
	int startdate_year;
	int startdate_month;
	int startdate_day;
	sscanf(departDate, "%04d-%02d-%02d", &startdate_year, &startdate_month,
			&startdate_day);
	int enddate_year;
	int enddate_month;
	int enddate_day;
	sscanf(backDate, "%04d-%02d-%02d", &enddate_year, &enddate_month,
			&enddate_day);

	if (startdate_year > enddate_year) {
		iserror = true;
	} else if (startdate_year == enddate_year) {
		if (startdate_month > enddate_month) {
			iserror = true;
		} else if (startdate_month == enddate_month) {
			if (startdate_day > enddate_day) {
				iserror = true;
			}
		}
	}
	return iserror;
}
